/**
 * Created by xuelei.kong on 2016/5/24.
 */
$(function () {
        var table= $('#dataTable').dataTable({
            "bInfo": false,
            "bFilter": false,
            "bPaginate": false,
            "bScrollCollapse": true,
            "aoColumns": [
                null, null, null, null, null
            ]
        });
    function setPeopleListHeight() {
        $('.table-responsive').height(document.documentElement.clientHeight * 0.7);
    }

    function winResize(fn) {
        var timer;
        clearTimeout(timer);
        $(window).resize(function () {
            timer = setTimeout(fn, 300);
        });
    }

    setPeopleListHeight();
    winResize(setPeopleListHeight);
   /*新增相册*/
    $('#add_pictures').on("click",function () {
        var tr='<tr><td><i class=" icon-folder-close"></i><a class="pictures_item" href="pictures_list"><span>New Picture</span><input class="edit_input" type="text"/></a><a  class="picture_option delete_picture" href="javascript:void(0)"><i class="icon-trash"></i></a><a class="picture_option edit_name" href="javascript:void(0)"><i class="icon-edit"></i></a></td><td>22	2 months ago</td> </tr>';
        $('#dataTable tbody').append(tr);
        table._fnAddTr(tr);
    })

    $('#dataTable tbody').on("mouseover","tr",function () {
      $(this).find(".picture_option").show();
    })
    $('#dataTable tbody').on("mouseleave","tr",function () {
        $(this).find(".picture_option").hide();
    })


    $('#dataTable tbody').on("click",".edit_name",function () {
        var tr=$(this).parents('tr');
        tr.find(".edit_input").show().focus();
        tr.find(".pictures_item").bind('click',function () {
            return false;
        })


    });
    $('#dataTable tbody').on("blur",".edit_input",function () {
        var tr=$(this).parents('tr'),name=tr.find('.pictures_item span')
        tr.find(".pictures_item").unbind('click')
        tr.find(".edit_input").hide();
        if($.trim(this.value)){
            name.text(this.value);
        }
    })

    var totalPage=10;
    function pagination(total,curr,callback){
        var page_btn='<div class="pagination">';
        if(curr==1){
            page_btn+='<a class="pg-prev pg-prev-disabled"><i class="icon-double-angle-left"></i></a>';
        }else{
            page_btn+='<a data-page="'+(curr-1)+'" href="javascript:void(0)" class="pg-prev"><i class="icon-double-angle-left"></i></a>';
        }
        if(curr-1>3){
            page_btn+='<a data-page="1" href="javascript:void(0)" class="pg-page pg-item">1</a>';
            page_btn+='<span class="pg-item">…</span>';
            for(var j=curr-2;j<curr;j++){
                page_btn+='<a data-page="'+j+'" href="javascript:void(0)" class="pg-page pg-item">'+j+'</a>';
            }
        }else{
            for(var i=1;i<curr;i++){
                page_btn+='<a data-page="'+i+'" href="javascript:void(0)" class="pg-page pg-item">'+i+'</a>';
            }

        }
        page_btn+='<span class="pg-current pg-item">'+curr+'</span>';
        if(curr+3>total){
            for(var k=curr;k<total;k++){
                page_btn+='<a data-page="'+(k+1)+'" href="javascript:void(0)" class="pg-page pg-item">'+(k+1)+'</a>';
            }
        }else{
            page_btn+='<a data-page="'+(curr+1)+'" href="javascript:void(0)" class="pg-page pg-item">'+(curr+1)+'</a>';
            page_btn+='<a data-page="'+(curr+2)+'" href="javascript:void(0)" class="pg-page pg-item">'+(curr+2)+'</a>';
            if(curr+3<total){
                page_btn+='<span class="pg-item">…</span>';
            }
            page_btn+='<a data-page="'+total+'" href="javascript:void(0)" class="pg-page pg-item">'+total+'</a>';

        }

        if(curr==total){
            page_btn+='<a class="pg-next pg-next-disabled"><i class="icon-double-angle-right"></i></a>';
        }else{
            page_btn+='<a data-page="'+(curr+1)+'" href="javascript:void(0)" class="pg-next"><i class="icon-double-angle-right"></i></a>';
        }
        page_btn+="</div>";
        typeof callback=='function' && callback();
        $('#pagination').empty().append(page_btn);

    }
    pagination(totalPage,1);
    $('#pagination').on('click',"a",function () {
        var page=$(this).data('page');
        page &&  pagination(totalPage,page,getData(page));
    })

    function api() {
        var t = this;
        var fnCallBack = arguments[1];
        var setting = {
            type: "GET",
            url: t+"?time="+(+new Date()),
            dataType: "json",
            data: arguments[0],
            beforeSend: function () {
              /*  loadingIcon.show();
                loadFlag = false;*/
            },
            success: function (data) {
                // loadingIcon.hide();
                fnCallBack(data);
            }
        }
        $.ajax(setting);
    }
    function getData(page) {
        api.call("people_data.json",{"page":page},function (data) {
            console.log(data);
            var tr='';
            $.each(data.data,function (idx, item) {
                console.log(item);
                tr+='<tr><td><i class=" icon-folder-close"></i><a class="pictures_item" href="pictures_list"><span>'+item["Picture Libraries"]+'</span><input class="edit_input" type="text"/></a><a  class="picture_option delete_picture" href="javascript:void(0)"><i class="icon-trash"></i></a><a class="picture_option edit_name" href="javascript:void(0)"><i class="icon-edit"></i></a></td><td>'+item["Items Last Modified"]+'</td> </tr>';
            })
            $('#dataTable tbody').empty().append(tr);
            table._fnClearTable();
            table._fnAddTr(tr);

        })
    }


})
